package job

import (
	"github.com/bgmp/config"
	"github.com/bgmp/model"
	"time"
)

func RefreshBgs(userId uint, start time.Time, end time.Time) {
	condition := model.BgSelectConditions{
		Start: model.Time{
			Time: start,
		},
		End: model.Time{
			Time: end,
		},
	}

	bgs := model.ListBgs(config.DB,
		map[string]any{model.Bg_UserId: userId},
		&condition,
	)

	for _, bg := range bgs {
		//bg.Time = time.Date(2024, 8, 17, bg.CreatedAt.Hour(), bg.CreatedAt.Minute(), bg.CreatedAt.Second(), 0, start.Location())

		bg.Time = bg.Time.AddDate(0, 0, 1)

		bg.Updates(config.DB)
	}
}

func RefreshDiets(userId uint, start time.Time, end time.Time) {
	condition := model.DietSelectConditions{
		Start: model.Time{
			Time: start,
		},
		End: model.Time{
			Time: end,
		},
	}

	diets := model.ListDiets(config.DB,
		map[string]any{model.Bg_UserId: userId},
		&condition,
	)

	for _, diet := range diets {
		//diet.CreatedAt = time.Date(2024, 8, 17, diet.CreatedAt.Hour(), diet.CreatedAt.Minute(), diet.CreatedAt.Second(), 0, start.Location())
		diet.CreatedAt = diet.CreatedAt.AddDate(0, 0, 1)

		_ = diet.Updates(config.DB)
	}
}

func RefreshHearts(userId uint, start time.Time, end time.Time) {
	condition := model.HeartSelectConditions{
		Start: model.Time{
			Time: start,
		},
		End: model.Time{
			Time: end,
		},
	}

	hearts := model.ListHearts(config.DB,
		map[string]any{model.Bg_UserId: userId},
		&condition,
	)

	for _, heart := range hearts {
		//diet.CreatedAt = time.Date(2024, 8, 17, diet.CreatedAt.Hour(), diet.CreatedAt.Minute(), diet.CreatedAt.Second(), 0, start.Location())
		//heart.UpdatedAt = diet.CreatedAt.AddDate(0, 0, 1)

		_ = heart.Updates(config.DB)
	}
}

func RefreshExercise(userId uint, start time.Time, end time.Time) {
	condition := model.ExerciseSelectConditions{
		Start: model.Time{
			Time: start,
		},
		End: model.Time{
			Time: end,
		},
	}

	hearts := model.ListExercise(config.DB,
		map[string]any{model.Bg_UserId: userId},
		&condition,
	)

	for _, heart := range hearts {
		//diet.CreatedAt = time.Date(2024, 8, 17, diet.CreatedAt.Hour(), diet.CreatedAt.Minute(), diet.CreatedAt.Second(), 0, start.Location())
		heart.CreateAt = heart.CreateAt.AddDate(0, 0, 1)
		_ = heart.Updates(config.DB)
	}
}
